﻿using Asp.Versioning;
using FlyingEye.ComServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [Route("api/device-ops-service/v{version:apiVersion}/come-users")]
    [ApiVersion("1.0")]
    [ControllerName("ComeUser")]
    [Authorize]
    public class ComeUserController : DefaultController
    {
        private readonly ComeUserService _comeUserService;

        public ComeUserController(ComeUserService comeUserService)
        {
            _comeUserService = comeUserService;
        }

        [HttpGet("{id}", Name = "GetByComeUserId")]
        public async Task<ActionResult<ComeUserResponse>> GetAsync(string id)
        {
            return await GetInvokeAsync(async () =>
            {
                return await _comeUserService.GetAsync(id);
            });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(string id)
        {
            return await DeleteInvokeAsync(async () =>
            {
                await _comeUserService.DeleteAsync(id);
            });
        }

        [HttpPost]
        public async Task<ActionResult<ComeUserResponse>> InsertAsync([FromBody] CreateComeUserRequest user)
        {
            return await InsertInvokeAsync(async () =>
            {
                return await _comeUserService.InsertAsync(user);
            }, "GetByComeUserId", (user) => new { id = user.Id });
        }
    }
}
